/*
 * Copyright 2024 Hangzhou Yingyi Technology Co., Ltd
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0

 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __RASPI_MBOX_H__
#define __RASPI_MBOX_H__

/* A properly aligned buffer */
#define MBOX_BUFFER_LENGTH	36

#define MBOX_REQUEST    0

/* Channels */
#define MBOX_CH_POWER   0
#define MBOX_CH_FB      1
#define MBOX_CH_VUART   2
#define MBOX_CH_VCHIQ   3
#define MBOX_CH_LEDS    4
#define MBOX_CH_BTNS    5
#define MBOX_CH_TOUCH   6
#define MBOX_CH_COUNT   7
#define MBOX_CH_PROP    8

/* Tags */
#define MBOX_TAG_SETPOWER       0x28001
#define MBOX_TAG_SETCLKRATE     0x38002
#define MBOX_TAG_LAST           0

#define MBOX_FULL       0x80000000
#define MBOX_EMPTY      0x40000000
#define MBOX_RESPONSE_ERROR    0x80000001
#define MBOX_RESPONSE_NORMAL   0x80000000

/**
 * The following table shows the register offsets for the different mailboxes.
 * Mailbox Read/Write Peek  Sender  Status    Config
 *  0      0x00       0x10  0x14    0x18      0x1c
 *  1      0x20
 */
#define MBOX_READ        0x0
#define MBOX_POLL        0x10
#define MBOX_SENDER      0x14
#define MBOX_STATUS      0x18
#define MBOX_CONFIG      0x1C
#define MBOX_WRITE       0x20

/**
 * @brief Make a mailbox call.
 *
 * @param ch channel index of raspi, only 8 work.
 *
 * @return -1: Mailbox not initialized, error
 *	    0: recv wrong response code 0x80000001
 *          1: recv right response code 0x80000000
 */
int mbox_call(unsigned char ch);

/**
 * @brief Parsing .dtb file to get base address of raspi mailbox reg.
 *
 * @param fdtp address of .dtb file.
 *
 */
void _libraspiplat_init_mbox(void *fdtp);

/**
 * @brief Creates a message to be sent to the VideoCore (VC) GPU.
 *
 * @param paras_num Number of elements to be passed to the mbox array.
 * @param ...	Variable number of parameters
 *		representing specific elements for the mbox array.
 *             - The second parameter specifies the size of the data to be sent.
 *             - The third parameter is either MBOX_REQUEST or MBOX_RESPONSE.
 *             - The fourth parameter represents the specific command.
 *             - Followed by specific parameters tagged accordingly.
 *             - The last parameter is always fixed to MBOX_TAG_LAST (0).
 *
 * @return No explicit return value. The message is created in the mbox array.
 * @note For specific msg format details, please refer to
 *	 the [Mailbox Property Interface]
 * (https://github.com/raspberrypi/firmware/wiki/Mailbox-property-interface)
 */
void mbox_msg_create(int paras_num, ...);

extern void clean_and_invalidate_dcache_range(
		unsigned long addr, unsigned long length);

#endif /* __RASPI_MBOX_H__ */
